import {
  addSave,
  getInfoByProductOrPartCode,
  getUpdateWarrantyDateApplyLogList,
  updateCodeWarrantyApply,
  updateDepartmentByCodeIds,
  updateUserCheck,
  updateWarrantyDateRangeCheck,
  updateWarrantyRange,
} from '@/api/code'
import { ref } from 'vue'

/**
 * 根据条码获取产品信息和登记信息
 */
export const useGetInfoByProductOrPartCode = () => {
  // 获取信息加载
  const getInfoLoading = ref(false)

  // 条码信息
  const codeInfo = ref<CodeProductOrPartSnCodeInfoByCode>({
    address: '',
    area: '',
    city: '',
    code: '',
    goodsBrand: '',
    goodsCode: '',
    goodsId: '',
    goodsModel: '',
    goodsName: '',
    goodsSpecId: '',
    goodsSpecName: '',
    goodsThreeType: '',
    goodsTwoType: '',
    goodsType: '',
    id: '',
    name: '',
    overInsured: '',
    phone: '',
    province: '',
    status: 1,
    town: '',
    type: 2,
    userId: '',
    warrantyDay: 30,
    warrantyEndTime: '',
    warrantyStartTime: '',
  })

  /**
   * 获取产品及公司登记信息
   * @param {String} code - 条码
   */
  const getInfo = async (code: string) => {
    getInfoLoading.value = true
    const res = await getInfoByProductOrPartCode(code)
    codeInfo.value = res.data.data.rows
    getInfoLoading.value = false
  }

  return {
    codeInfo,
    getInfoLoading,
    getInfo,
  }
}

/**
 * 创建条码
 */
export const useCreate = () => {
  // 创建加载
  const createLoding = ref(false)

  // 创建表单
  const form = ref<CodeAPIRequest.Create>({
    codeVos: [
      {
        goodsId: '',
        goodsSpecId: '',
        qty: '1',
        warrantyDay: 30,
      },
    ],
    name: '',
    qty: '',
    type: 1,
  })

  /**
   * 创建方法
   */
  const create = async () => {
    createLoding.value = true
    const res = await addSave(form.value)
    createLoding.value = false
    return res.data
  }

  /**
   * 表单重置
   */
  const resetForm = () => {
    form.value = {
      codeVos: [
        {
          goodsId: '',
          goodsSpecId: '',
          qty: '1',
          warrantyDay: 30,
        },
      ],
      name: '',
      qty: '',
      type: 1,
    }
  }

  return {
    form,
    createLoding,
    create,
    resetForm,
  }
}

/**
 * 申请更新条码的质保周期（网点使用）
 */
export const useUpdateCodeWarrantyApply = () => {
  // 更新表单
  const updateForm = ref({
    codeId: '',
    newEndTime: '',
    newStartTime: '',
  })

  // 更新加载
  const updateLoading = ref(false)

  /**
   * 更新事件
   */
  const update = async () => {
    updateLoading.value = true
    const res = await updateCodeWarrantyApply(updateForm.value)
    updateLoading.value = false
    return res.data
  }

  /**
   * 重置表单
   */
  const resetForm = () => {
    updateForm.value = {
      codeId: '',
      newEndTime: '',
      newStartTime: '',
    }
  }

  return {
    updateForm,
    updateLoading,
    update,
    resetForm,
  }
}

/**
 * 更新条码的质保周期（总部使用）
 */
export const useUpdateWarrantyRange = () => {
  // 更新表单
  const updateForm = ref<CodeAPIRequest.UpdateWarrantyDateRange>({
    id: '',
    warrantyEndDate: '',
    warrantyStartDate: '',
    ids: [],
  })

  // 更新加载
  const updateLoading = ref(false)

  /**
   * 更新事件
   */
  const update = async () => {
    updateLoading.value = true
    const res = await updateWarrantyRange(updateForm.value)
    updateLoading.value = false
    return res.data
  }

  /**
   * 重置表单
   */
  const resetForm = () => {
    updateForm.value = {
      id: '',
      warrantyEndDate: '',
      warrantyStartDate: '',
      ids: [],
    }
  }

  return {
    updateForm,
    updateLoading,
    update,
    resetForm,
  }
}

/**
 * 更改客户审核
 */
export const useUpdateUserCheck = () => {
  // 审核加载
  const checkLoading = ref(false)

  // 审核表单
  const checkFrom = ref<CodeAPIRequest.UpdateUserCheck>({
    applyMsg: '',
    applyState: 1,
    id: '',
  })

  /**
   * 重置表单
   */
  const resetForm = () => {
    checkFrom.value = {
      applyMsg: '',
      applyState: 1,
      id: '',
    }
  }

  /**
   * 审核
   */
  const check = async () => {
    checkLoading.value = true
    const res = await updateUserCheck(checkFrom.value)
    checkLoading.value = false
    return res.data
  }

  return {
    checkFrom,
    checkLoading,
    resetForm,
    check,
  }
}

/**
 * 获取申请更改质保周期日志列表
 */
export const useGetUpdateWarrantyDateRangeLogList = () => {
  // 获取列表加载
  const getListLoading = ref(false)

  // 列表
  const list = ref<Array<UpdateWarrantyDateRangeApplyLog>>([])

  /**
   * 获取列表
   * @param {string} codeId - 条码id
   * @returns {Promise<string>}
   */
  const getList = async (codeId: string) => {
    getListLoading.value = true
    const res = await getUpdateWarrantyDateApplyLogList(codeId)
    getListLoading.value = false
    if (res.data.code === 20000) {
      list.value = res.data.data
      return ''
    }
    return res.data.message
  }

  return {
    list,
    getListLoading,
    getList,
  }
}

/**
 * 审核更改质保周期
 */
export const useUpdateWarrantyDateRangeCheck = () => {
  // 审核加载
  const checkLoading = ref(false)

  // 审核表单
  const checkForm = ref<{
    applyMsg: string
    applyState: 1 | 2
    id: string
  }>({
    applyMsg: '',
    applyState: 1,
    id: '',
  })

  /**
   * 审核
   */
  const check = async () => {
    checkLoading.value = true
    const res = await updateWarrantyDateRangeCheck(checkForm.value)
    checkLoading.value = false
    return res.data
  }

  /**
   * 重置表单
   */
  const resetForm = () => {
    checkForm.value = {
      applyMsg: '',
      applyState: 1,
      id: '',
    }
  }

  return {
    checkForm,
    checkLoading,
    check,
    resetForm,
  }
}

/**
 * 根据条码id更新部门
 */
export const useUpdateDepartmentByCodeIds = () => {
  // 更新加载
  const updateLoading = ref(false)

  // 更新表单
  const updateForm = ref<{ codeId: Array<string>; departmentIds: Array<string> }>({
    codeId: [],
    departmentIds: [],
  })

  /**
   * 更新
   */
  const update = async () => {
    updateLoading.value = true
    const res = await updateDepartmentByCodeIds(updateForm.value)
    updateLoading.value = false
    return res.data
  }

  /**
   * 重置更新表单
   */
  const resetUpdateForm = () => {
    updateForm.value = {
      codeId: [],
      departmentIds: [],
    }
  }

  return {
    updateForm,
    updateLoading,
    update,
    resetUpdateForm,
  }
}
